package popcorn.ticket.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.entity.Collections;
import popcorn.ticket.service.CollectionsService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Controller
public class CollectionsController {
	@Autowired
	private CollectionsService cs;

	// 查询所有+分页+通过电影编号查询
	@GetMapping(value = "ctpage", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public PageData<Collections> sltCollByMovieId(
			@RequestParam(value = "movieId", defaultValue = "0", required = false) int movieId,
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "limit", defaultValue = "10", required = false) int limit) {

		return cs.selectByMovieId(movieId, page, limit);
	}

	// 添加收藏
	@PostMapping(value = "addct",consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse addCollections(@RequestBody Collections collections) {

		return cs.insert(collections);
	}

	// 删除收藏
	@GetMapping(value = "delct", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse delCollections(int collectid) {

		return cs.deleteByPrimaryKey(collectid);
	}
	
	// 获取单个用户收藏列表
	@RequestMapping("getUserCollect/{userid}")
	@ResponseBody
	public List<Collections> getUserCollection(@PathVariable("userid") int userid,
			@RequestParam(defaultValue = "1")int page) {
		PageHelper.startPage(page,3);
		List<Collections> list = cs.selectByUserId(userid);
		return list;
	}
	
	// 获取用户所有收藏
	@RequestMapping("countUserCollect/{userid}")
	@ResponseBody
	public int countUserCollection(@PathVariable("userid") int userid) {
		List<Collections> list = cs.selectByUserId(userid);
		PageInfo<Collections> pi = new PageInfo<Collections>(list);
		return (int)pi.getTotal();
	}
	
	// 用户删除收藏
	@RequestMapping(value = "delCollection/{collectid}" , method = RequestMethod.DELETE)
	@ResponseBody
	public AjaxResponse delUserCollection(@PathVariable("collectid") int collectid) {
		return cs.deleteByPrimaryKey(collectid);
	}
	
	// 收藏排行榜
	@RequestMapping(value = "gcl", produces = "application/json")
	@ResponseBody
	public PageData<Collections> getCollectionList(@RequestParam(value = "page", defaultValue = "1") int page) {
		PageData<Collections> pd = cs.getList(page);
		return pd;
	}
	
	// 根据用户id和电影id删除收藏
	@PostMapping(value = "delctbyum", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse delByUM(@RequestBody Collections record) {

		return cs.deleteByUMId(record);
	}

    //用于判断用户是否收藏该电影
	@PostMapping(value = "docoll", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public int doColl(@RequestBody Collections record) {

		return cs.docoll(record);
	}

	
}
